package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.LogisticsAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.CommentListRequest;
import com.dinadi.yidi.bean.CommentRequest;
import com.dinadi.yidi.bean.LogisticsBean;
import com.dinadi.yidi.bean.LogisticsListRequest;
import com.dinadi.yidi.bean.LogisticsRequest;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.MyHttpCallback;
import com.dinadi.yidi.http.interfaces.LogisticsInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * author: JiaYu.He
 * Date: 2017/7/12   物流详情
 * Time: 17:57
 */
public class LogisticsDetailsActivity extends BaseActivity {
    private View view;
    private TextView tvTitle,tv_logistics_company, tv_waybill;
    private ListView logistics_listview;
    private LogisticsAdapter adapter;
    private List<LogisticsRequest> mList = new ArrayList<>();

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(LogisticsDetailsActivity.this).inflate(R.layout.activity_logistics_details, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_alogistics_track);

        logistics_listview = (ListView) view.findViewById(R.id.logistics_listview);
        logistics_listview.setDivider(null);
        tv_logistics_company = (TextView) view.findViewById(R.id.tv_logistics_company);
        tv_waybill = (TextView) view.findViewById(R.id.tv_waybill);
        adapter = new LogisticsAdapter(LogisticsDetailsActivity.this, mList);
        logistics_listview.setAdapter(adapter);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String courierNum = intent.getStringExtra("courierNum");
        LogisticsBean bean = new LogisticsBean();
        tv_waybill.setText(courierNum);
//        bean.setNu("420839036985");
        /*物流单号*/
        bean.setNu(courierNum);
        bean.setCom("auto");
        LogisticsBean bean2 = new LogisticsBean();
        bean2.setAuthorization("APPCODE " + "1016190c714b4190b4ac9ee69225ea02");
        LogisticsInterface logisticsInterface = new LogisticsInterface(LogisticsDetailsActivity.this, httpListener);
        logisticsInterface.getInterface(bean2, bean);
//        OkHttpUtils.get()
//                .url("http://ali-deliver.showapi.com/showapi_expInfo")
//                .addHeader("Authorization","APPCODE 1016190c714b4190b4ac9ee69225ea02")
//                .addParams("nu","883420070249072469")
//                .addParams("com","auto")
//                .build()
//                .connTimeOut(5000)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e) {
//                        toast( e.toString());
//                    }
//
//                    @Override
//                    public void onResponse(String response) {
//
//                        toast(response);
//                    }
//                });

    }

    @Override
    protected void clickEvent(View v) {

    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String showapi_res_body = JsonUtil.getFieldValue(response, "showapi_res_body");
            String ret_code = JsonUtil.getFieldValue(showapi_res_body, "ret_code");
            if (ret_code.equals("0")) {
                LogisticsListRequest  logisticsList = JsonUtil.parseJsonToBean(showapi_res_body, LogisticsListRequest.class);
                mList.addAll(logisticsList.getData());
                adapter.notifyDataSetChanged();
                String expTextName = JsonUtil.getFieldValue(showapi_res_body, "expTextName");
                tv_logistics_company.setText(expTextName);
            } else {
                toast("物流查询失败");
            }

        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast("物流查询失败");
        }
    };
    /*"mailNo": "968018776110",//快递单号
        "update": 1466926312666,//数据最后查询的时间
		"updateStr": "2016-06-26 15:31:52",//数据最后更新的时间
		"ret_code": 0,//接口调用是否成功,0为成功,其他为失败
		"flag": true,//物流信息是否获取成功
		"status": 4,-1 待查询 0 查询异常 1 暂无记录 2 在途中 3 派送中 4 已签收 5 用户拒签 6 疑难件 7 无效单
 8 超时单 9 签收失败 10 退回
		"tel": "400-889-5543",//快递公司电话
		"expSpellName": "shentong",//快递字母简称
		"data": [//具体快递路径信息
*/
}
